<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\controller\v1\finance;

use app\services\user\UserBillServices;
use think\facade\App;
use app\adminapi\controller\AuthController;
use think\facade\Db;
/**
 * Class Finance
 * @package app\adminapi\controller\v1\finance
 */
class Finance extends AuthController
{
    /**
     * Finance constructor.
     * @param App $app
     * @param UserBillServices $services
     */
    public function __construct(App $app, UserBillServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 筛选类型
     */
    public function bill_type()
    {
        return app('json')->success($this->services->bill_type());
    }

    /**
     * 资金记录
     */
    public function list()
    {
        $where = $this->request->getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
            ['limit', 20],
            ['page', 1],
            ['type', ''],
        ]);
        return app('json')->success($this->services->getBillList($where));
    }

    /**
     * 佣金记录
     * @return mixed
     */
    public function get_commission_list()
    {
        $where = $this->request->getMore([
            ['nickname', ''],
            ['price_max', ''],
            ['price_min', ''],
            ['sum_number', 'normal'],
            ['brokerage_price', 'normal'],
            ['time', '']
        ]);
        return app('json')->success($this->services->getCommissionList($where));
    }

    /**
     * 佣金详情用户信息
     * @param $id
     * @return mixed
     */
    public function user_info($id)
    {
        return app('json')->success($this->services->user_info((int)$id));
    }

    /**
     * 佣金提现记录个人列表
     */
    public function get_extract_list($id = '')
    {
        if ($id == '') return app('json')->fail(100100);
        $where = $this->request->getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', '']
        ]);
        $where['category'] = 'now_money';
        $where['type'] = ['brokerage', 'brokerage_user'];
        return app('json')->success($this->services->getBillOneList((int)$id, $where));
    }

    /*
     * 资金流水
     * 2024年3月4日10:50:29
     * */
    public function fundFlow(){
        $where = $this->request->getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['order_no', ''],
            ['limit', 10],
            ['page', 1],
            ['type', ''],
            ['phone', ''],
            ['name', ''],
        ]);
        $page = $where['page'];
        $where2 = [];
        if(!empty($where['start_time']) && !empty($where['end_time']) ){
            $where2[] = ["f.add_time",">=",$where['start_time']];
            $where2[] = ["f.add_time","<=",$where['end_time']];
        }
        if(!empty($where['type'])){
            $where2[] = ["f.type","=",$where['type']];
        }
        if(!empty($where['order_no'])){
            $where2[] = ["o.order_no","like","%".$where['order_no']."%"];
        }
        if(!empty($where['phone'])){
            $where2[] = ["o.contact_mobile","like","%".$where['phone']."%"];
        }
        if(!empty($where['name'])){
            $where2[] = ["o.contact_name","like",'%'.$where['name'].'%'];
        }
        $size = $where['limit'];
        $data = Db::name('order_flow')
            ->alias('f')
            ->field('f.remarks,f.id as f_id,f.order_id as f_order_id,f.type as f_type,f.amount as f_amount,f.add_time as f_add_time,f.account as f_account,o.illegal_deposit,o.deposit,o.shifu_car_fee,o.coupon_price,o.order_no,o.pay_time,o.type_name,o.contact_name,
            o.contact_mobile,o.add_time,o.start_time,o.end_time,p.title as store_name,o.pay_time,o.price,t.car_no,o.book_day as tianshu')
            ->limit((($page - 1) * $size),$size)->order('f.id desc')
            ->join('car_order o','f.order_id = o.id','left')
            ->join('car_order_take t','t.id=o.take_id','left')
            ->join('base_place p','p.id=o.store_id','left')
            ->where($where2)
            ->select()->toArray();
        foreach ($data as $k=>$v) {
            $data[$k]['add_time'] = $v['add_time'] ? date('Y年m月d日 H时i分', $v['add_time']) : '暂无';
            $data[$k]['pay_status'] = $v['pay_time'] ? '已支付' : '未支付';
            $data[$k]['start_time'] = $v['start_time'] ? date('m月d日 H时i分', $v['start_time']) : '暂无';
            $data[$k]['end_time'] = $v['end_time'] ? date('m月d日 H时i分', $v['end_time']) : '暂无';
            $data[$k]['time'] = date('m月d日 H时i分', $v['start_time']);
            $data[$k]['time2'] = date('m月d日 H时i分', $v['end_time']);
            $data[$k]['f_add_time'] = $v['f_add_time'] ? date('Y年m月d日 H时i分s秒', $v['f_add_time']) : '暂无';
            $data[$k]['pay_time'] = $v['pay_time'] ? date('Y年m月d日 H时i分s秒', $v['pay_time']) : '暂无';
            if($v['f_type']==1){
                $data[$k]['f_typename'] = '订单款';
            }elseif($v['f_type']==21){
                $data[$k]['f_typename'] = '车辆押金退款';
            }elseif($v['f_type']==31){
                $data[$k]['f_typename'] = '违章押金退款';
            }else{
                $data[$k]['f_typename'] = '取消订单退款';
            }
        }

        $data2 = Db::name('order_flow')
            ->alias('f')
            ->field('f.remarks,f.id as f_id,f.order_id as f_order_id,f.type as f_type,f.amount as f_amount,f.add_time as f_add_time,f.account as f_account,o.illegal_deposit,o.deposit,o.shifu_car_fee,o.coupon_price,o.order_no,o.pay_time,o.type_name,o.contact_name,
            o.contact_mobile,o.add_time,o.start_time,o.end_time,p.title as store_name,o.pay_time,o.price,t.car_no,o.book_day as tianshu')
            ->join('car_order o','f.order_id = o.id','left')
            ->join('car_order_take t','t.id=o.take_id','left')
            ->join('base_place p','p.id=o.store_id','left')
            ->where($where2)
            ->select()->toArray();
        $res = [];
        $res['list'] = $data;
        $res["page"]=$where['page'];
        $res["count"]=count($data2);
        $res["size"]=$size;
        return app('json')->success($res);
    }
    //获得流水类型
    public function get_fund_type()
    {
        $data = Db::name('fund_type')->where('state',1)->select()->toArray();
        return app('json')->success($data);
    }

    }
